/* 
 * Metro Dynamis Core Web
 * Version: 1.0.6
 * Author: Jose E. Gonzalez Modecir 
 * License: https://metrodynamis.com/product/metrodynamis-core-web/license/
 * http://metrodynamis.com
 * 
 */

(function (MD, $, undefined) {
  MD.version = "1.0";

  //Public namespaces
  MD.Helpers = {};

  MD.UI = {};

  MD.UI.Nav = {
    fixPositions: function () {
      $('.md-nav:visible').each(function () {
        var el = $(this).contents().find('li');
        var eo = new Array();
        for (var i = 0; i < el.length; i++) eo[i] = $(el[i]).position();
        for (var i = 0; i < el.length; i++) {
          $(el[i]).css({ position: 'absolute', top: eo[i].top, left: eo[i].left });
        }
      });

    }

  };

  MD.UI.Viewport = {
    setAllHeight: function () {
      $('.md-viewport-vertical:visible').each(function () {
        var vo = $(this).offset();
        $(this).height($(window).height() - vo.top);
        $(this).children('.md-scrollable').height(MD.UI.Calc.elementsHeight($('.md-scrollable')) + 40);
      });
    },

    setAllWidth: function () {
      $('.md-viewport-horizontal:visible').each(function () {
        var vo = $(this).offset();
        $(this).height($(window).height() - vo.top);
      });
      $('.md-auto-resize:visible').each(function () {
        $(this).width(MD.UI.Calc.elementsWidth(this));
      });
      $('.md-auto-max-width:visible').each(function () {
        var mw = 0;
        $(this).children().each(function () {
          var tw = $(this).outerWidth(true);
          if (tw > mw) mw = tw;
        });
        $(this).width(mw);
      });
      $('.md-viewport-horizontal:visible > .md-scrollable').each(function () {
        $(this).width(MD.UI.Calc.elementsWidth(this) + 120);
      });
      $('.md-viewport-horizontal:visible').mousewheel(function (event, delta) {
        this.scrollLeft -= (delta * 40);
        event.preventDefault();
      });
    },

    setAllOffset: function () {
      var left = 0;
      $('.md-viewport-horizontal:visible').each(function () {
        $(this).css('left', left + 'px');
        left = left + $(this).width();
      });
    }

  };

  MD.UI.List = {
    autoArrangeTiles: function () {
      $('.md-auto-arrange-tiles:visible').each(function () {
        if ($(this).height == 0) return;
        var eOffset = $(this).offset();
        var hParent = Math.floor($(window).height() - eOffset.top - 40);
        $(this).height(hParent);
        var hItem = Math.floor($(this).find('.md-list-item:first-child').outerHeight(true));
        var wItem = Math.floor($(this).find('.md-list-item:first-child').outerWidth(true));
        var mRows = Math.floor(hParent / hItem);
        var mColums = Math.ceil($(this).children().length / mRows);
        var sWidth = Math.floor(mColums * wItem / 10) * 10 + 10;
        $(this).width(sWidth);
      });

    }

  };

  MD.UI.Columns = {
    autoArrangeColumns: function () {
      $('.md-auto-columns:visible').each(function () {
        if ($(this).children().hasClass('md-column')) return;
        var eOffset = $(this).offset();
        var hParent = Math.floor($(window).height() - eOffset.top - 40);
        var el = $(this).children();
        $(this).empty();
        var j = 0;
        var cc = new Array();
        cc[j] = $('<div class="md-column"></div>');
        $(cc[j]).appendTo(this);
        var i = 0;
        while (i <= $(el).length) {
          $(el[i]).appendTo(cc[j]);
          if ($(cc[j]).outerHeight(true) > hParent) {
            $(el[i]).remove();
            j++;
            cc[j] = $('<div class="md-column"></div>');
            $(cc[j]).appendTo(this);
            $(el[i]).appendTo(cc[j]);
          }
          i++;
        }
        $(this).width(MD.UI.Calc.elementsWidth(this));
      });

    }

  };

  MD.UI.Calc = {
    elementsWidth: function (element) {
      var m = 0;
      $(element).children().each(function () {
        m = m + $(this).outerWidth(true);
      });
      return m;
    },

    elementsHeight: function (element) {
      var m = 0;
      $(element).children().each(function () {
        m = m + $(this).outerHeight(true);
      });
      return m;
    }
  };

  MD.UI.recalculateAll = function () {
    var fns = [MD.UI.Columns.autoArrangeColumns, MD.UI.List.autoArrangeTiles, MD.UI.Viewport.setAllWidth, MD.UI.Viewport.setAllHeight];
    for (i = 0; i < fns.length; i++) { fns[i](); }
  };


  MD.Initialize = function () {
    MD.UI.recalculateAll();
    MD.UI.Nav.fixPositions();
  };

} (window.MD = window.MD || {}, jQuery));

$(document).ready(function () {
   MD.Initialize();
});

// Plugins included keeping all in one file, final user don't have to deal with all this

// Mouse wheel
/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * under the MIT license (http://www.opensource.org/licenses/mit-license.php)
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);